package com.mzp.XbShop.controller;

import com.mysql.jdbc.TimeUtil;
import com.mzp.XbShop.common.entity.PageResult;
import com.mzp.XbShop.common.entity.Result;
import com.mzp.XbShop.common.entity.StatusCode;
import com.mzp.XbShop.pojo.Goods;
import com.mzp.XbShop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author admin
 * @Description goods 控制器层
 * @date 2021-04-17 15:04:51
 */
@RestController
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(StatusCode.OK, true, "查询成功", goodsService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(StatusCode.OK, true, "查询成功", goodsService.findById(id));
    }

    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Goods> pageList = goodsService.findSearch(searchMap, page, size);
        return new Result(StatusCode.OK, true, "查询成功", new PageResult<Goods>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        return new Result(StatusCode.OK, true, "查询成功", goodsService.findSearch(searchMap));
    }


    @RequestMapping(value = "/searchOne/{groupid}", method = RequestMethod.GET)
    public Result findByGroupid(@PathVariable String groupid) {
        return new Result(StatusCode.OK, true, "查询成功", goodsService.findByGroupid(groupid));
    }

    /**
     * 增加
     *
     * @param goods
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Goods goods) {
        goodsService.add(goods);
        return new Result(StatusCode.OK, true, "增加成功");
    }

    /**
     * 修改
     *
     * @param goods
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody Goods goods, @PathVariable String id) {
        goods.setId(id);
        goodsService.update(goods);
        return new Result(StatusCode.OK, true, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        goodsService.deleteById(id);
        return new Result(StatusCode.OK, true, "删除成功");
    }

    //查找四个最新发布的商品
    @RequestMapping(value = "/search/newlist", method = RequestMethod.GET)
    public Result newlist() {
        return new Result(StatusCode.OK, true, "查询成功", goodsService.newlist());
    }

    //    //查找四个热门的商品
//    @RequestMapping(value = "/search/hotlist",method = RequestMethod.GET)
//    public Result hotlist() {
//        return new Result(StatusCode.OK, true, "查询成功", goodsService.hotlist());
//    }
//查找四个热门的商品
    @RequestMapping(value = "/search/hotlist", method = RequestMethod.GET)
    public Result hotlist() {


        return new Result(StatusCode.OK, true, "查询成功", goodsService.hotlist());
    }

    //模糊查询
    @RequestMapping(value = "/searchByName/{title}", method = RequestMethod.GET)
    public Result searchByName(@PathVariable String title) {
        return new Result(StatusCode.OK, true, "查询成功", goodsService.findByTitle("%" + title + "%"));
    }

    //提升 浏览量
    @RequestMapping(value = "/updateVisitors/{id}/{userId}", method = RequestMethod.GET)
    public Result updateVisitors(@PathVariable String id, @PathVariable String userId) {
        //设置redis获取userid
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        Object flag = redisTemplate.opsForValue().get("goodsid" + id);
        if (flag != null) {
//            if(!redisTemplate.hasKey("goodsid" + id)){
//                goodsService.updateVisitors(id);
//                //重新设定过期时间
//                redisTemplate.opsForValue().set("goodsid"+id,userId,180L, TimeUnit.MINUTES);
//                return new Result(StatusCode.OK, true, "浏览量+1");
//            }else {
            return new Result(StatusCode.ERROR, false, "180分钟后+1");
//            }
        }
        goodsService.updateVisitors(id);
        redisTemplate.opsForValue().set("goodsid" + id, userId, 180L, TimeUnit.MINUTES);
        return new Result(StatusCode.OK, true, "浏览量+1");
    }
}
